package application;


import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import framework.DBTool;

public class RegisterFrame extends JFrame{

	/**
	 * The serial number.
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * The register window width in pixels.
	 */
	static private int REGISTER_WIDTH = 300;
	
	/**
	 * The register window height in pixels.
	 */
	static private int REGISTER_HEIGHT = 200;
	/**
	 * Main window width in pixels.
	 */
	static private int MAIN_WIDTH = 780;

	/**
	 * Main window height in pixels.
	 */
	static private int MAIN_HEIGHT = 600;
	
	
	private JLabel titleLabel;
	private JLabel userNameLabel;
	private JLabel passwordLabel;
	private JTextField userName;
	private JPasswordField password;
	private JPanel inputPanel;
	private JPanel buttonPanel;
	private JButton btnLogin;
	private JButton btnRegister;
	
	public RegisterFrame() {
		titleLabel = new JLabel("用户登录/注册");
		userNameLabel = new JLabel("用户：");
		passwordLabel = new JLabel("密码：");
		btnLogin = new JButton("登录");
		btnRegister = new JButton("注册");
		userName = new JTextField();
		password = new JPasswordField();
		
		inputPanel = new JPanel();
		inputPanel.setLayout(new GridLayout(2,2,0,0));
		inputPanel.add(userNameLabel);
		inputPanel.add(userName);
		inputPanel.add(passwordLabel);
		inputPanel.add(password);
		
		buttonPanel = new JPanel();
		buttonPanel.add(btnLogin);
		buttonPanel.add(btnRegister);
		
		
		titleLabel.setHorizontalAlignment(SwingConstants.CENTER); 
		userNameLabel.setHorizontalAlignment(SwingConstants.CENTER); 
		passwordLabel.setHorizontalAlignment(SwingConstants.CENTER); 
		
		setLayout(new GridLayout(3,1,0,0));
		add(titleLabel);
		add(inputPanel);
		add(buttonPanel);
		
		btnLogin.addActionListener(new LoginListener());
		btnRegister.addActionListener(new RegisterListener());
		
	}
	
	/**
	 * This inner class processes <code>btnLogin</code> events.
	 *
	 */
	class LoginListener implements ActionListener {

		JFrame warningFrame;
		
		public void actionPerformed(ActionEvent event) {
			
			String strName = userName.getText();
			String strPassword = String.valueOf(password.getPassword());
			
			warningFrame = new JFrame();
			warningFrame.setLocationRelativeTo(null);
			JLabel warningLabel1 = new JLabel("用户不存在！");
			JLabel warningLabel2 = new JLabel("用户名或密码错误！");
			JButton btnRetry = new JButton("重试");
			JPanel warningPanel = new JPanel();
			warningFrame.setContentPane(warningPanel);
			warningFrame.setLocationRelativeTo(null);
			warningFrame.setVisible(false);
			
			btnRetry.addActionListener(new RetryListener());
			
			try {
				if(!DBTool.isAccountExist(strName)) {
					warningPanel.add(warningLabel1);
					warningPanel.add(btnRetry);
					warningFrame.setVisible(true);
				}
				else if(!DBTool.isAccessible(strName, strPassword)) {
					warningPanel.add(warningLabel2);
					warningPanel.add(btnRetry);
					warningFrame.setVisible(true);
				}
				else {
					warningFrame.dispose();
					dispose();
					
					JFrame playFrame;
					
					playFrame = new PlayerFrame(strName,strPassword);
					playFrame.setTitle("音乐播放器MusicPlayer");
					playFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					playFrame.setSize(MAIN_WIDTH, MAIN_HEIGHT);
					playFrame.setResizable(true);
					playFrame.setVisible(true);
					playFrame.setLocationRelativeTo(null);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
		
		/**
		 * 
		 *This inner class processes <code>btnRetry</code> events in the login frame.
		 *
		 */
		class RetryListener implements ActionListener {
			
			public void actionPerformed(ActionEvent event) {
				warningFrame.dispose();
			}
			
		}
		
	}
	
	/**
	 * 
	 * This inner class processes <code>btnRegister</code> events.
	 *
	 */
	class RegisterListener implements ActionListener {
		
		JFrame newFrame;
		JTextField userName = new JTextField();
		JPasswordField password = new JPasswordField();
		
		
		
		public void actionPerformed(ActionEvent event) {
			
			newFrame = new JFrame("注册");
			JLabel titleLabel = new JLabel("注册新用户");
			JLabel userNameLabel = new JLabel("用户");
			JLabel passwordLabel = new JLabel("密码");
			JPanel inputPanel = new JPanel(new GridLayout(2,2,0,0));
			JPanel buttonPanel = new JPanel();
			JButton btnConfirm = new JButton("确认");
			
			inputPanel.add(userNameLabel);
			inputPanel.add(userName);
			inputPanel.add(passwordLabel);
			inputPanel.add(password);
			
			buttonPanel.add(btnConfirm);
			
			titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
			userNameLabel.setHorizontalAlignment(SwingConstants.CENTER);
			passwordLabel.setHorizontalAlignment(SwingConstants.CENTER);
			
			newFrame.setLayout(new GridLayout(3,1,0,0));
			newFrame.add(titleLabel);
			newFrame.add(inputPanel);
			newFrame.add(buttonPanel);
			newFrame.setSize(REGISTER_WIDTH, REGISTER_HEIGHT);
			newFrame.setLocationRelativeTo(getOwner());
			newFrame.setVisible(true);
			
			btnConfirm.addActionListener(new ConfirmListener());
			
			
		}
		
		/**
		 * 
		 * This inner class processes <code>btnConfirm</code> events in the register frame.
		 *
		 */
		class ConfirmListener implements ActionListener {
			
			JFrame reminderFrame;
			
			public void actionPerformed(ActionEvent event) {
				
				String strName = userName.getText();
				String strPassword = String.valueOf(password.getPassword());
				
				JLabel reminderLabel1 = new JLabel("注册成功，请重新登录！");
				JLabel reminderLabel2 = new JLabel("账号已存在或输入不恰当！");
				JButton confirmButton = new JButton("确认");
				JButton retryButton = new JButton("重试");
				JPanel reminderPanel = new JPanel();
				
				reminderFrame = new JFrame();
				reminderFrame.setLocationRelativeTo(null);
				reminderFrame.setContentPane(reminderPanel);
				reminderFrame.setLocationRelativeTo(null);
				reminderFrame.setVisible(false);
				
				confirmButton.addActionListener(new ConfirmListener2());
				retryButton.addActionListener(new RetryListener());
				
				try {
					if(DBTool.createAccount(strName, strPassword)) {
						reminderPanel.add(reminderLabel1);
						reminderPanel.add(confirmButton);
						reminderFrame.setVisible(true);
						
					}
					else {
						reminderPanel.add(reminderLabel2);
						reminderPanel.add(retryButton);
						reminderFrame.setVisible(true);
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
				

				
			}
			
			/**
			 * 
			 * This inner class processes <code>confirmButton</code> events in reminder frame.
			 *
			 */
			class ConfirmListener2 implements ActionListener {
				
				public void actionPerformed(ActionEvent event) {
					
					reminderFrame.dispose();
					newFrame.dispose();
					
				}
			}
			
			/**
			 * 
			 * This inner class processes <code>retryButton</code> events in reminder frame.
			 *
			 */
			class RetryListener implements ActionListener {
				
				public void actionPerformed(ActionEvent event) {
					
					reminderFrame.dispose();
					
				}
				
			}
			
			
		}
		
		
	}
	
	
	
}
